#!/bin/bash

# postinst script for system-kernel-dtrace-providers
#
# see: dh_installdeb(1)

#set -e

# summary of how this script can be called:
#        * <postinst> \`configure' <most-recently-configured-version>
#        * <old-postinst> \`abort-upgrade' <new version>
#        * <conflictor's-postinst> \`abort-remove' \`in-favour' <package>
#          <new-version>
#        * <postinst> \`abort-remove'
#        * <deconfigured's-postinst> \`abort-deconfigure' \`in-favour'
#          <failed-install-package> <version> \`removing'
#          <conflicting-package> <version>
# for details, see http://www.debian.org/doc/debian-policy/ or
# the debian-policy package

PATH=/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin
export PATH

if [ "${BASEDIR:=/}" = "/" ]; then
    BASEDIR=""
else
    BASEDIR_OPT="-b $BASEDIR"
fi

case "$1" in
    configure)
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/dcpc.conf ] && rm -f $BASEDIR/kernel/drv/dcpc.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/dcpc.conf"
	    chown root:sys "$BASEDIR/kernel/drv/dcpc.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/dtrace.conf ] && rm -f $BASEDIR/kernel/drv/dtrace.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/dtrace.conf"
	    chown root:sys "$BASEDIR/kernel/drv/dtrace.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/fasttrap.conf ] && rm -f $BASEDIR/kernel/drv/fasttrap.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/fasttrap.conf"
	    chown root:sys "$BASEDIR/kernel/drv/fasttrap.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/fbt.conf ] && rm -f $BASEDIR/kernel/drv/fbt.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/fbt.conf"
	    chown root:sys "$BASEDIR/kernel/drv/fbt.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/lockstat.conf ] && rm -f $BASEDIR/kernel/drv/lockstat.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/lockstat.conf"
	    chown root:sys "$BASEDIR/kernel/drv/lockstat.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/profile.conf ] && rm -f $BASEDIR/kernel/drv/profile.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/profile.conf"
	    chown root:sys "$BASEDIR/kernel/drv/profile.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/sdt.conf ] && rm -f $BASEDIR/kernel/drv/sdt.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/sdt.conf"
	    chown root:sys "$BASEDIR/kernel/drv/sdt.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/systrace.conf ] && rm -f $BASEDIR/kernel/drv/systrace.conf
	else
	    chmod 0644 "$BASEDIR/kernel/drv/systrace.conf"
	    chown root:sys "$BASEDIR/kernel/drv/systrace.conf"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/dcpc ] && rm -f $BASEDIR/kernel/drv/amd64/dcpc
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/dcpc"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/dcpc"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/dtrace ] && rm -f $BASEDIR/kernel/drv/amd64/dtrace
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/dtrace"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/dtrace"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/fasttrap ] && rm -f $BASEDIR/kernel/drv/amd64/fasttrap
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/fasttrap"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/fasttrap"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/fbt ] && rm -f $BASEDIR/kernel/drv/amd64/fbt
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/fbt"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/fbt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/lockstat ] && rm -f $BASEDIR/kernel/drv/amd64/lockstat
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/lockstat"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/lockstat"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/profile ] && rm -f $BASEDIR/kernel/drv/amd64/profile
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/profile"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/profile"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/sdt ] && rm -f $BASEDIR/kernel/drv/amd64/sdt
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/sdt"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/sdt"
	fi
	if [ "$ZONEINST" = "1" ]; then
	    [ -f $BASEDIR/kernel/drv/amd64/systrace ] && rm -f $BASEDIR/kernel/drv/amd64/systrace
	else
	    chmod 0755 "$BASEDIR/kernel/drv/amd64/systrace"
	    chown root:sys "$BASEDIR/kernel/drv/amd64/systrace"
	fi
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/dcpc dcpc)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/fasttrap fasttrap)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/fbt fbt)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/lockstat lockstat)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/profile profile)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/sdt sdt)
	[ "$ZONEINST" = "1" ] || (mkdir -p $BASEDIR/kernel/dtrace/amd64 && cd $BASEDIR/kernel/dtrace/amd64 && cp -f ../../../kernel/drv/amd64/systrace systrace)
	[ "$ZONEINST" = "1" ] || (grep -c "^dcpc " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  dcpc ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^dtrace " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0666 root sys' dtrace ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^fasttrap " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m 'fasttrap 0666 root sys' fasttrap ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^fbt " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m 'fbt 0644 root sys' fbt ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^lockstat " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m '* 0644 root sys' lockstat ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^profile " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m 'profile 0644 root sys' profile ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^sdt " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m 'sdt 0644 root sys' sdt ) )
	[ "$ZONEINST" = "1" ] || (grep -c "^systrace " $BASEDIR/etc/name_to_major >/dev/null || ( add_drv -n  $BASEDIR_OPT  -m 'systrace 0644 root sys' systrace ) )
	
    ;;

    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    *)
        echo "postinst called with unknown argument '$1'" >&2
        exit 1
    ;;
esac



# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

#DEBHELPER#

exit 0

